﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using GalaSoft.MvvmLight;
using System.IO;

namespace Scripni.EditPathVariable.Core
{
	/// <summary>
	/// Represents the model of a path element.
	/// </summary>
	public class PathElement : ObservableObject, IDataErrorInfo
	{
		public PathElement()
			: this(string.Empty)
		{
		}

		public PathElement(string path)
		{
			Name = path;
		}
		private string name;
		public string Name
		{
			get { return this.name; }
			set
			{
				if (value != this.name)
				{
					this.name = value;
					RaisePropertyChanged(() => Name);
				}
			}
		}

		public string Error
		{
			get 
            {
                //the only field that can cause an error is the 'Name' field
                if(this[NameField] != null)
                {
                    return string.Format(InvalidNameErrorMessage, Name);
                }

                return null;
            }
		}

	    private const string NameField = "Name";

	    private const string InvalidNameErrorMessage = "The path {0} cannot be found.";

		public string this[string columnName]
		{
			get
			{
				switch (columnName)
				{
					case NameField:
						if (!(string.IsNullOrEmpty(Name) || Directory.Exists(Name)))
						{
							return "Invalid path!";
						}
						break;

				}
				return null;
			}
		}
	}
}
